package autoteam.ui.dialogs;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import autoteam.tables.AutoTeamTableModel;
import db.ColumnData;
import db.DBRecord;
import db.DataAccessObject;

public class CoachCreator extends AutoTeamDialog
{
    
    private static final long serialVersionUID = -142007470596164860L;
    
    //buttons
    private JButton addButton;
    private JButton cancelButton;
    
    //text fields
    /*
     * Firstname:
     * Lastname: 
     * Team: 
     */
    private JTextField firstnameField;
    private JTextField lastnameField;
    private JComboBox teamBox;
    
    private AutoTeamTableModel model;
    
    public CoachCreator( JFrame dialogOwner, AutoTeamTableModel model )
    {
        super( dialogOwner );
        
        this.model = model;
        
        addButton = new JButton( "Add" );
        cancelButton = new JButton( "Cancel" );
        
        this.setTitle( "Add New Coach" );
        this.setDialogSize( new Dimension( 300, 125 ) );
        
        JPanel buttonPane = new JPanel( );
        buttonPane.add( addButton );
        buttonPane.add( cancelButton );
        
        JPanel formPane = new JPanel( new GridLayout( 3, 2 ) );
        JLabel firstLabel = new JLabel( "First name: " );
        JLabel lastLabel = new JLabel( "Last name: " );
        JLabel teamLabel = new JLabel( "Team name: " );
        
        firstnameField = new JTextField( 25 );
        lastnameField = new JTextField( 25 );
        teamBox = new JComboBox( );
        DataAccessObject dao = new DataAccessObject( );
        List <DBRecord> teamRecs = dao.getAllRecords( "teams" );
        for ( DBRecord rec : teamRecs )
            teamBox.addItem( rec.getFieldValue( "teamName" ) );
        
        formPane.add( firstLabel );
        formPane.add( firstnameField );
        formPane.add( lastLabel );
        formPane.add( lastnameField );
        formPane.add( teamLabel );
        formPane.add( teamBox );
        
        Container contentPane = getContentPane( );
        setButtonActions( this );
        contentPane.add( formPane, BorderLayout.CENTER );
        contentPane.add( buttonPane, BorderLayout.SOUTH );
        
        pack( );
        
    }
    
    @ Override
    protected void setButtonActions( final AutoTeamDialog thisDialog )
    {
        addButton.addActionListener( new ActionListener( )
        {
            
            @ Override
            public void actionPerformed( ActionEvent e )
            {
                DataAccessObject dao = new DataAccessObject( );
                String team = teamBox.getSelectedItem().toString();
                ColumnData col = new ColumnData("teamName", team);
                DBRecord rec = dao.getRecord("teams", col);
                int teamId = (Integer) rec.getFieldValue("id");
                dao.addRecord
                (       
                     //"coaches", new ColumnData( "teamId", teamBox.getSelectedIndex( ) ),
                     "coaches", new ColumnData("teamId", teamId),
                     new ColumnData( "firstname", firstnameField.getText( ) ),
                     new ColumnData( "lastname", lastnameField.getText( ) ),
                     new ColumnData( "picturepath", "./trunk/img/profile_default.gif" ),
                     new ColumnData( "biography", "(not available/created)")
                     // new ColumnData( "teamName", teamBox.getSelectedItem( ).toString( ) )
                     
                );
                model.add( 0 );
                model.fireTableDataChanged( );
                thisDialog.close( );
            }
            
        } );
        
        cancelButton.addActionListener( new ActionListener( )
        {
            
            @ Override
            public void actionPerformed( ActionEvent e )
            {
                thisDialog.close( );
            }
            
        } );
    }
    
}
